package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.UserRank;
import com.yanqu.road.entity.zhaoshang.*;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.utils.date.DateHelper;

public class ZhaoShangPb {

    public static ZhaoShangProto.ZsProjectInfoTemp.Builder parseZsProjectInfoTemp(ZsProject zsProject, boolean seatDetail, long playerId) {
        ZhaoShangProto.ZsProjectInfoTemp.Builder builder = ZhaoShangProto.ZsProjectInfoTemp.newBuilder();
        builder.setProjectId(zsProject.getProjectId());
        builder.setOpenTime(zsProject.getOpenTime());
        builder.setOpenUserId(zsProject.getOpenUserId());
        builder.setTemplateId(zsProject.getTemplateId());
        builder.setOpenUser(PlayerBasePb.parsePlayerBaseTempMsg(zsProject.getOpenUser()));
        builder.setOccupyNum(zsProject.getSeatUserMap().size() + 1); //包含立项人
        builder.setServerId(zsProject.getServerId());
        builder.setTitleAddition(zsProject.getTitleAddition());
        builder.setSkillAddition(0);
        if(zsProject.getAdditionData() != null){
            builder.setSkillAddition(zsProject.getAdditionData().getCurioAddition());
        }
        builder.setAddition(zsProject.getAddition());
        //称号是否过期
        int currentSecond = DateHelper.getCurrentSecond();
        if(zsProject.getDecorationOverTime() != 0 &&  currentSecond> zsProject.getDecorationOverTime()){
            builder.setDecorationOver(true);
        }
        builder.setDuration(zsProject.getDuration());
         for(int seatId : zsProject.getSeatUserMap().keySet()){
             ZsProjectSeat zsProjectSeat = zsProject.getSeatMap().get(seatId);
             if(zsProjectSeat.getUserId() == playerId) {
                 builder.setJoin(true);
                 if(!seatDetail){
                     break;
                 }
             }
             if(seatDetail) {
                 ZhaoShangProto.ZsProjectSeatInfo.Builder seatBuilder = ZhaoShangProto.ZsProjectSeatInfo.newBuilder();
                 seatBuilder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(zsProject.getSeatUserMap().get(seatId)));
                 seatBuilder.setId(seatId);
                 seatBuilder.setUserId(zsProjectSeat.getUserId());
                 seatBuilder.setAddition(zsProjectSeat.getAddition());
                 seatBuilder.setSitTime(Long.valueOf(zsProjectSeat.getSitTime()).longValue());
                 seatBuilder.setServerId(zsProjectSeat.getServerId());
                 seatBuilder.setSendReward(zsProjectSeat.isSendReward());
                 if(zsProjectSeat.getDecorationOverTime() != 0 &&  currentSecond > zsProjectSeat.getDecorationOverTime()){
                     seatBuilder.setDecorationOver(true);
                 }
                 builder.addSeatList(seatBuilder);
             }
         }
        return builder;
    }


    public static ZhaoShangProto.ZsWarReportInfoTemp.Builder parseToZsWarReportTemp(ZsWarReport report) {
        ZhaoShangProto.ZsWarReportInfoTemp.Builder builder = ZhaoShangProto.ZsWarReportInfoTemp.newBuilder();
        builder.setProjectId(report.getProjectId());
        builder.setAddTime(report.getAddTime());
        builder.setType(report.getType());
        builder.setDuration(report.getDuration());
        if(report.getEnemyBaseInfo() != null) {
            builder.setEnemyInfo(PlayerBasePb.parsePlayerBaseTempMsg(report.getEnemyBaseInfo()));
        }
        builder.setTemplateId(report.getTemplateId());
        builder.setEnemyUserId(report.getEnemyUserId());
        if(report.getReward() != null) {
            builder.setReward(report.getReward());
        }
        builder.setSeatId(report.getSeatId());
        builder.setSitTime(report.getSitTime());
        builder.setReaded(report.isReaded());
        builder.setUserId(report.getUserId());
        if(report.getUserBaseInfo() != null) {
            builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(report.getUserBaseInfo()));
        }
        builder.setDecorationOver(report.isDecorationOver());
        builder.setOpenUserId(report.getOpenUserId());
        if(report.getOpenDecoration() != null) {
            builder.setOpenDecoration(report.getOpenDecoration());
        }
        builder.setOpenTitleId(report.getOpenTitleId());
        return builder;
    }

    public static ZhaoShangProto.ZsWarReportInfoTemp.Builder parseToZsWarReportTemp(ZsProjectReward reward) {
        ZhaoShangProto.ZsWarReportInfoTemp.Builder builder = ZhaoShangProto.ZsWarReportInfoTemp.newBuilder();
        builder.setProjectId(reward.getProjectId());
        builder.setAddTime(reward.getAddTime());
        builder.setType(reward.getType());
        builder.setDuration(reward.getDuration());
        if(reward.getEnemyBaseInfo() != null) {
            builder.setEnemyInfo(PlayerBasePb.parsePlayerBaseTempMsg(reward.getEnemyBaseInfo()));
        }
        builder.setTemplateId(reward.getTemplateId());
        builder.setEnemyUserId(reward.getEnemyUserId());
        if(reward.getReward() != null) {
            builder.setReward(reward.getReward());
        }
        builder.setSeatId(reward.getSeatId());
        builder.setSitTime(reward.getSitTime());
        builder.setUserId(reward.getUserId());
        if(reward.getUserBaseInfo() != null) {
            builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(reward.getUserBaseInfo()));
        }
        return builder;
    }


    public static ZsWarReport parseToZsWarReport(ZhaoShangProto.ZsWarReportInfoTemp zsWarReportTemp) {
        ZsWarReport report =  new ZsWarReport();
        report.setProjectId(zsWarReportTemp.getProjectId());
        report.setAddTime(zsWarReportTemp.getAddTime());
        report.setType(zsWarReportTemp.getType());
        report.setDuration(zsWarReportTemp.getDuration());
        if(zsWarReportTemp.hasEnemyInfo() && zsWarReportTemp.getEnemyInfo().getServerId() > 0) {
            report.setEnemyBaseInfo(PlayerBasePb.parseToUserBaseInfo(zsWarReportTemp.getEnemyInfo()));
        }
        report.setTemplateId(zsWarReportTemp.getTemplateId());
        report.setEnemyUserId(zsWarReportTemp.getEnemyUserId());
        report.setReward(zsWarReportTemp.getReward());
        report.setSeatId(zsWarReportTemp.getSeatId());
        report.setSitTime(zsWarReportTemp.getSitTime());
        report.setReaded(zsWarReportTemp.getReaded());
        report.setUserId(zsWarReportTemp.getUserId());
        if(zsWarReportTemp.hasUserInfo()) {
            report.setUserBaseInfo(PlayerBasePb.parseToUserBaseInfo(zsWarReportTemp.getUserInfo()));
        }
        report.setDecorationOver(zsWarReportTemp.getDecorationOver());
        report.setOpenUserId(zsWarReportTemp.getOpenUserId());
        report.setOpenDecoration(zsWarReportTemp.getOpenDecoration());
        report.setOpenTitleId(zsWarReportTemp.getOpenTitleId());
        return report;
    }

    public static RankProto.RankTempMsg.Builder parseRankTempMsg(ZsTopOfRank userRank){
        if(null == userRank){
            return null;
        }
        RankProto.RankTempMsg.Builder msg = RankProto.RankTempMsg.newBuilder();
        msg.setUserId(userRank.getUserId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(userRank.getUserBaseInfo());
        if(null != baseMsg) {
            msg.setPlayerBaseData(baseMsg);
        }
        msg.setValue(String.valueOf(userRank.getValue()));
        msg.setParam(userRank.getParam());
        return msg;
    }

    public static ZhaoShangProto.ZsWeekRankTemp.Builder parseZsWeekRankTemp(UserRank userRank) {
        ZhaoShangProto.ZsWeekRankTemp.Builder builder = ZhaoShangProto.ZsWeekRankTemp.newBuilder();
        RankProto.RankTempMsg.Builder rankTemp = CrossRankPb.parseRankTempMsg(userRank);
        builder.setRankType(userRank.getType());
        builder.setData(rankTemp);
        return builder;
    }

    public static ZhaoShangProto.ZsProjectSeatSimpleInfo parseZsProjectSeatSimpleInfo(int seatId, ZsProjectSeat zsProjectSeat, UserBaseInfo userBaseInfo) {
        ZhaoShangProto.ZsProjectSeatSimpleInfo.Builder builder = ZhaoShangProto.ZsProjectSeatSimpleInfo.newBuilder();
        builder.setId(seatId);
        builder.setServerId(userBaseInfo.getServerId());
        builder.setUserId(zsProjectSeat.getUserId());
        builder.setNickName(userBaseInfo.getNickName());
        return  builder.build();
    }
}
